<?php		            	
require 'auth.php';
require '../config/config.php';
mydb_connect();

$bio = $conn->quote($_POST['bio']);

if($_POST['firstname']==" " || $_POST['lastname']==" " || $_POST['twitter_id']){
	echo "error|| Some fields aren't accepted to be empty. ";
	exit();
}
$path = "../../assets/img/authors/";

	$valid_formats = array("jpg", "png", "gif", "bmp","JPG","PNG","GIF","BMP","jpeg","JPEG");
	//if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
		//{
			$name = $_FILES['image']['name'];
			$size = $_FILES['image']['size'];
			$r=10;
			if(strlen($name))
				{
					$r =1;
					list($txt, $ext) = explode(".", $name);
					$ext = pathinfo($name, PATHINFO_EXTENSION);
					if(in_array($ext,$valid_formats))
					{
						$r =2;
					if($size < (1024*1024*10))
						{
							$r =3;
							
							$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
							$tmp = $_FILES['image']['tmp_name'];
								
									$r =4;
								$insert_into_users=$conn->prepare("INSERT INTO `users`(`firstname`,`lastname`,`role`) VALUES(:firstname,:lastname,:role)");
								$insert_into_users->execute(array("firstname" => $_POST['firstname'], "lastname" => $_POST['lastname'], "role" => 'ext_author'));
								if(!$insert_into_users){
									echo "error||" . $insert_into_users->errorInfo();
									exit();
								}
								$select_last = $conn->query("SELECT * FROM `users` ORDER BY `id` DESC LIMIT 1");
								$last_row = $select_last->fetch();
								$insert=$conn->query("INSERT INTO `user_authors`(`id`,`twitter_id`,`bio`,`facebook_url`,`twitter_url`,`linkedin_url`,`image`,`role`) VALUES('".$last_row['id']."','".$_POST['twitter_id']."','$bio','".$_POST['facebook_url']."','http://twitter.com/".$_POST['twitter_id']."','".$_POST['linkedin_url']."', '$actual_image_name','external')");	
									//echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
								if(!$insert){
									echo "error||" . $insert->errorInfo();
									exit();
								}
								
							if($insert){
								move_uploaded_file($tmp, $path.$actual_image_name);
								$select_last1 = $conn->query("SELECT * FROM `user_authors` ORDER BY `id` DESC LIMIT 1");
								$last_row1 = $select_last1->fetch();
								echo "<option value='".$last_row1['id']."' selected>".$last_row['firstname']. " " . $last_row['lastname']." - ".$last_row1['twitter_id']."</option>";
								exit();	
							}
							else{
								echo "error||" . $insert->errorInfo();
								exit();
							}
						}
						else
						{echo "error|| Image file size max 10 MB";$r=88;}					
						}
						else
						{echo "error|| Invalid file format..";$r=99;}	
				} else {
								$insert_into_users=$conn->prepare("INSERT INTO `users`(`firstname`,`lastname`,`role`) VALUES(:firstname,:lastname,:role)");
								$insert_into_users->execute(array("firstname" => $_POST['firstname'], "lastname" => $_POST['lastname'], "role" => 'ext_author'));
								if(!$insert_into_users){
									echo "error||" . $insert_into_users->errorInfo();
									exit();
								}
								$select_last = $conn->query("SELECT * FROM `users` ORDER BY `id` DESC LIMIT 1");
								$last_row = $select_last->fetch();
								$insert=$conn->query("INSERT INTO `user_authors`(`id`,`twitter_id`,`bio`,`facebook_url`,`twitter_url`,`linkedin_url`,`role`) VALUES('".$last_row['id']."','".$_POST['twitter_id']."','$bio','".$_POST['facebook_url']."','http://twitter.com/".$_POST['twitter_id']."','".$_POST['linkedin_url']."','external')");	
									//echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
								if(!$insert){
									echo "error||" . $insert->errorInfo();
									exit();
								}
								
							if($insert){
								move_uploaded_file($tmp, $path.$actual_image_name);
								$select_last1 = $conn->query("SELECT * FROM `user_authors` ORDER BY `id` DESC LIMIT 1");
								$last_row1 = $select_last1->fetch();
								echo "<option value='".$last_row1['id']."' selected>".$last_row['firstname']. " " . $last_row['lastname']." - ".$last_row1['twitter_id']."</option>";
								exit();	
							}
				}
			?>